<?php
	//error_reporting(E_ALL);
	//ini_set('display_errors', '1');

	// config definition

	define('MENUID',1);
	define('SITEID',1);
	
	include 'db.inc.php';
		
	class databaseconfig {
		function connectconfig () {
			global $db;
			
			$this->conn = mysql_connect($db['server'], $db['username'], $db['password']);
			
			if (!$this->conn) {
				echo "
				<p>
					MySQL Error #" . mysql_errno() . "
				</p>
				n
				<p>
					" . mysql_error() ."
				</p>
				n"; 
			}
		
			$this->selectDBconfig($db['name']);
		}
		
		function selectDBconfig ($dbname) {
			if (!mysql_select_db($dbname,$this->conn)) {
				echo "<p>MySQL Error #" . mysql_errno() . "</b></p>n<p>" . mysql_error() ."</p>n"; 
			}
		}
		
		/*METODO PARA HACER CONSULTAS*/
		function queryconfig ($query) {
			$result = mysql_query($query);
			
			if (!$result) {
				echo $query . 'could not be completed.';
			}
		
			return $result;
		}
		
		function select($query, $maxRows=0, $pageNum=0){
			$this->queryconfig = $query;
			
			// start limit if $maxRows is greater than 0
			if($maxRows>0){
				$startRow = $pageNum * $maxRows;
				$query = sprintf("%s LIMIT %d, %d", $query, $startRow, $maxRows);
			}	

			$result = mysql_query($query, $this->conn);	

			if ($this->error()) die ($this->debug());
				$output=false;
				
			for ($n=0; $n < mysql_num_rows($result); $n++){
				$row = mysql_fetch_assoc($result);
				$output[$n] = $row;
			}
			
			return $output;
		} 
		
		function debug($type="", $action="", $tablename=""){
			switch ($type){
				case "connect":
					$message = "MySQL Error Occured";
					$result = mysql_errno() . ": " . mysql_error();
					$query = "";
					$output = "Could not connect to the database. Be sure to check that your database connection settings are correct and that the MySQL server in running.";
					break;
				
				case "array":
					$message = $action." Error Occured";
					$result = "Could not update ".$tablename." as variable supplied must be an array.";
					$query = "";
					$output = "Sorry an error has occured accessing the database. Be sure to check that your database connection settings are correct and that the MySQL server in running.";
					break;

				default:
					if (mysql_errno($this->conn)){
						$message = "MySQL Error Occured";
						$result = mysql_errno($this->conn) . ": " . mysql_error($this->conn);
						$output = "Sorry an error has occured accessing the database. Be sure to check that your database connection settings are correct and that the MySQL server in running.";
					} else {
						$message = "MySQL Query Executed Succesfully.";
						$result = mysql_affected_rows($this->conn) . " Rows Affected";
						$output = "view logs for details";
					}
				
					$linebreaks = array("\n", "\r");
					
					if($this->query != "") $query = "QUERY = " . str_replace($linebreaks, " ", $this->query); else $query = "";
					break;
			}
		
			$output = "<b style='font-family: Arial, Helvetica, sans-serif; color: #0B70CE;'>".$message."</b><br />\n<span style='font-family: Arial, Helvetica, sans-serif; color: #000000;'>".$result."</span><br />\n<p style='Courier New, Courier, mono; border: 1px dashed #666666; padding: 10px; color: #000000;'>".$query."</p>\n";
	
			return $output;
		}


		function error(){
			if (mysql_errno($this->conn)) return true; else return false;
		}
	}//Fin de clase

	$database2 = new databaseconfig();
	$database2->connectconfig();

	if($_GET['site'] != ''){
		$_SESSION['site']['rid'] = $_GET['site'];
		$_SESSION['site']['grid'] = $_GET['site'];
	}
	
	if(isset($_SESSION['site'])){
		if($_SESSION['site']['grid'] == '' ){
			$_SESSION['site']['grid'] = 1;	
			$_SESSION['site']['grid'] = 1;		
			setcookie ("grid",1,  $time+604800,"/");
		}
	} else{
		$_SESSION['site']['grid'] = 1;	
		$_SESSION['site']['grid'] = 1;		
			setcookie ("grid",1,  $time+604800,"/");
	}			


	define("RID", $_SESSION['site']['grid']);
	
	$config_query = $database2->queryconfig("select * from config where id = ". $_SESSION['site']['grid'] ." ");
	$config = mysql_fetch_array($config_query);

	//Find data variables		
	define('DELIVERY',$config['delivery']);
	define('ACCEPT_CREDIT',$config['credit']);
	define('ADMIN_USERNAME',$config['username']);
	define('ADMIN_PASSWORD',$config['password']);
	define('CURRENCY',$config['money_symbol']);
	define('CURRENCY_POS',$config['money_position']);
	define('ADMIN_EMAIL',$config['email']);
	define('SITE_NAME',$config['sitename']);
	define('TAX_PERCENT',$config['tax']);
	define('DELIVERY_FEE',$config['deliveryfee']);
	define('HOMECONTENT',$config['home']);
	define('TEMPLATE',$config['template']);		
	define('REG',$config['reg']);	
	define('ROOT',$root);
	define('SITEROOT',$siteroot);		

	//dont mess with anything below this	
	require ''.$root.'core/smarty/Smarty.class.php';

	$smarty = new Smarty;	
	
	$smarty->assign('title',$config['title']);
	$smarty->assign('description',$config['description']);
	$smarty->assign('keywords',$config['keywords']);

	$_SESSION['frid'] = 1;

	function showSerialForm(){
		echo '
		<form action="index.php" method="post" style="font-size:1.4em;padding:50px">
			Please enter serial: <input  type="text" name="serial"> <input type="submit">
		</form>';	
	}
	
?>